package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.MompTMMcFundInfoMapper;
import cc.rengu.igas.momp.common.entity.MompTMMcFundInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 营销活动打款记录表数据库操作接口实现
 *
 * @author liujinan
 * @date 2020/9/18 22:50
 */

public class MompTMMcFundInfoMapperImpl implements MompTMMcFundInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    @Override
    public List<MompTMMcFundInfo> selectMompTMMcFundInfoByInstIdAndMcNoAndDescByCreateTime(String instId, String mcNo, String payerAcctId, String payerAcctType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.info("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectTMMcFundInfoByMcNoAnd");

        MompTMMcFundInfo MompTMMcFundInfo = new MompTMMcFundInfo();
        MompTMMcFundInfo.setInstId(instId);
        MompTMMcFundInfo.setMcNo(mcNo);
        MompTMMcFundInfo.setPayerAcctId(payerAcctId);
        MompTMMcFundInfo.setPayerAcctType(payerAcctType);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, MompTMMcFundInfo, MompTMMcFundInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据机构号和营销活动编号查询营销活动打款记录失败,instId<{}>,mcNo<{}>", instId, mcNo);
            return null;
        }
        return objectList.stream().map(item -> (MompTMMcFundInfo) item).collect(Collectors.toList());
    }
}
